package parse

import (
	"net/http"
	"net/url"
	"strings"

	"gitee.com/samhula/gospider/config"

	"github.com/PuerkitoBio/goquery"
)

// Parse 解析Html
type Parse struct {
	Document    goquery.Document //goquery doc
	CurrentPage Page
}

// NewParse 生成Parse结构
func NewParse(response *http.Response) (*Parse, error) {
	doc, err := goquery.NewDocumentFromResponse(response)
	if err != nil {
		return nil, err
	}
	parse := &Parse{
		Document: *doc,
	}
	return parse, nil
}

// GetPage 获取当前页面内容
func (p *Parse) GetPage(ipConfig *config.IPConfig) (*Page, error) {
	//get next page url
	nextPageURL := p.GetRuleText(p.Document.Selection, ipConfig.NextPage, p.Document.Url)
	page := &Page{
		NextPageURL: nextPageURL,
		CurrentURL:  p.Document.Url.String(),
	}
	items := make([]*Item, 0)
	p.Document.Find(ipConfig.Items).NextAll().Each(func(i int, s *goquery.Selection) {
		item := &Item{}
		item.Address = p.GetRuleText(s, ipConfig.ItemAddress, p.Document.Url)
		item.Anonymous = p.GetRuleText(s, ipConfig.ItemAnonymous, p.Document.Url)
		item.IP = p.GetRuleText(s, ipConfig.ItemIP, p.Document.Url)
		item.Port = p.GetRuleText(s, ipConfig.ItemPort, p.Document.Url)
		item.Speed = p.GetRuleText(s, ipConfig.ItemSpeed, p.Document.Url)
		item.Time = p.GetRuleText(s, ipConfig.ItemTime, p.Document.Url)
		item.Type = p.GetRuleText(s, ipConfig.ItemType, p.Document.Url)
		items = append(items, item)
	})
	page.Items = items
	return page, nil
}

// GetRuleText get rule text
func (p *Parse) GetRuleText(s *goquery.Selection, ruleStr string, refURL *url.URL) string {
	rule, err := NewRule(ruleStr)
	if err != nil {
		return ""
	}
	content := rule.getContent(s, refURL)
	if len(content) == 0 || content == "" {
		return ""
	}
	return strings.TrimSpace(content)
}
